package com.lbs.jiepiaisi.biz;

import android.content.Intent;

import com.lbs.jiepiaisi.bean.AccountProblemEntity;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.AccountProblemParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2018/1/9 10:21
 * 作用：意见反馈
 */

public class AccountProblemBiz {
    public void accountProblem(String userId,String info,String contact,String phone,String app){

            FormBody formBody = new FormBody
                    .Builder()
                    .add("userId",userId)//用户ID
                    .add("info",info)//意见反馈
                    .add("contact",contact)//联系人
                    .add("phone",phone)//联系电话
                    .add("app",app)// QQ or WebChat
                    .build();
            HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_PROBLEM_URL,formBody, new HttpUtil.Listener() {
                @Override
                public void success(Response response) {
                    try{
                        String jsonString = response.body().string();//返回结果
                        JSONObject jsonObject = new JSONObject(jsonString);//转换结果
                        AccountProblemEntity accountProblemEntity= AccountProblemParser.parser(jsonObject.toString());//json解析
                        accountProblemEntity.setCode(Constant.STATUS_OK);
                        Intent intent = new Intent(Constant.ACCOUNT_PROBELM);
                        intent.putExtra(Constant.KEY_DATA, accountProblemEntity);
                        App.instance.sendBroadcast(intent);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                @Override
                public void failed() {
                    AccountProblemEntity accountProblemEntity=new AccountProblemEntity();
                    accountProblemEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                    Intent intent = new Intent(Constant.ACCOUNT_PROBELM);
                    intent.putExtra(Constant.KEY_DATA, accountProblemEntity);
                    App.instance.sendBroadcast(intent);
                }
                @Override
                public void noNetwork() {
                    AccountProblemEntity accountProblemEntity=new AccountProblemEntity();
                    accountProblemEntity.setCode(Constant.STATUS_NO_NET);
                    Intent intent = new Intent(Constant.ACCOUNT_PROBELM);
                    intent.putExtra(Constant.KEY_DATA, accountProblemEntity);
                    App.instance.sendBroadcast(intent);
                }
            });
    }
}
